package com.example.springboot.controller;

import com.example.springboot.controller.dto.JsonResponse;
import com.example.springboot.controller.dto.UserDTO;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public JsonResponse login(@RequestBody UserDTO UserDto, HttpServletRequest request) {
        List<User> userList = userService.login(UserDto);
        System.out.println(userList.size());
        if (userList.size() == 0 || userList == null) {
            return new JsonResponse<User>(1, "未查询到用户", null);
        }
        return new JsonResponse(0, "登录成功", userList.get(0).getUsername());
    }

    @PostMapping("")
    public Integer save(@RequestBody User user) {
        //新增或者更新
        return userService.save(user);
    }

    @GetMapping("")
    public List<User> findAll() {
        //查询全部数据
        return userService.findAll();
    }

    @DeleteMapping("/{id}")
    public  Integer delete(@PathVariable Integer id) {
        return userService.delete(id);
    }
}
